package com.example.ftptest;

import org.apache.ftpserver.DataConnectionConfigurationFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置类
 */
@Configuration
public class FtpConfig extends CachingConfigurerSupport {
    @Value("${ftp.port}")
    private Integer ftpPort;
    @Value("${ftp.passivePorts}")
    private String ftpPassivePorts;
    @Value("${ftp.passiveExternalAddress}")
    private String ftpPassiveExternalAddress;
    @Value("${ftp.ftpUserName}")
    private String ftpUserName;
    @Value("${ftp.ftpPassword}")
    private String ftpPassword;

    @Bean
    public FtpServer createFtpServer() throws FtpException {
        String homeDirectory = null;
        // 获取操作系统
        String osName = System.getProperty("os.name");

        // 如果是windows系統
        if (osName.toLowerCase().contains("windows")) {
            homeDirectory = "D:\\resource";
        } else {
            homeDirectory = "/home/resource";
        }
        // 判断该路径是否存在，如果不存在则创建
        File file = new File(homeDirectory);
        if (!file.exists()) {
            file.mkdirs();
        }

        FtpServerFactory serverFactory = new FtpServerFactory();
        ListenerFactory listenerFactory = new ListenerFactory();
        //设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端
        DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
        dataConnectionConfigurationFactory.setIdleTime(30 * 60);
        dataConnectionConfigurationFactory.setActiveLocalPort(ftpPort);
//        dataConnectionConfigurationFactory.setPassiveIpCheck(true);
//        dataConnectionConfigurationFactory.setPassivePorts(ftpPassivePorts);
//        dataConnectionConfigurationFactory.setPassiveExternalAddress(ftpPassiveExternalAddress);
        listenerFactory.setDataConnectionConfiguration(dataConnectionConfigurationFactory.createDataConnectionConfiguration());

        //替换默认的监听器
        serverFactory.addListener("default", listenerFactory.createListener());

        // 配置用戶

        BaseUser user = new BaseUser();
        user.setName(ftpUserName);
        user.setPassword(ftpPassword);
        user.setHomeDirectory(homeDirectory);
        //权限 增加写权限
        List<Authority> authorities = new ArrayList<>();
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);
        serverFactory.getUserManager().save(user);

        PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
        userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
        serverFactory.setUserManager(userManagerFactory.createUserManager());

        //增加该用户
        serverFactory.getUserManager().save(user);

        Map<String, Ftplet> m = new HashMap<>();
        m.put("miaFtplet", new UploadListener());

        serverFactory.setFtplets(m);
        // start the server
        FtpServer server = serverFactory.createServer();

        return server;
    }
}
